// 导入所有静态路由和动态路由
import { constantRoutes, otherRoutes } from '@/router'

const state = {
    // 初始的静态路由（登录页，首页，404等）
    routes: constantRoutes
}
const mutations = {
    // 设置用户路由为静态路由+自己所拥有的路由
    setRoutes(state, newRoutes) {
        state.routes = [...constantRoutes, ...newRoutes]
    }
}

const actions = {
    // 筛选出用户自己所拥有的路由
    // menus为当前用户所拥有的路由名称数组
    // otherRoutes为项目中所有的动态路由
    filterRoutes(context, menus) {
        const routes = []
        menus.forEach(item => {
            otherRoutes.forEach(route => {
                if(route.name === item) {
                    routes.push(route)
                }
            })
        })
        context.commit('setRoutes', routes)
        return routes
    }
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}

